import Store from '@/store'
/**
 * App
 */

/**
 * 初始化
 */
const init = async () => {
	console.log('【APP平台初始化完成】 ');
};

/**
 * 登录
 */
const login = (options) => {
	return new Promise(function(resolve, reject) {
		Store.dispatch('Login', {
				smsCodeKey: options.smsCodeKey,
				login: options.login
			}).then(res => {
				console.log('res: ', res);
				resolve(res);
			})
			.catch((err) => {
				reject(err)
			})
	});
};

/**
 * 微信登录
 */
const wxLogin = (options) => {
	return new Promise(function(resolve, reject) {
		uni.login({
			provider: 'weixin',
			timeout: 5000,
			success(res) {
				// console.log('res: ', res);
				//
				uni.getUserInfo({
					provider: 'weixin',
					withCredentials: true,
					lang: 'zh_CN',
					timeout: 5000,
					success(res) {
						const userInfo = res.userInfo
						// console.log('userInfo: ', userInfo);
						Store.dispatch('AppWxLogin', {
							appId: options.appId,
							openId: userInfo.openId,
							unionId: userInfo.unionId,
							nickname: userInfo.nickName,
							headimgurl: userInfo.avatarUrl,
						}).then(res => {
							resolve(res);
						}).catch(err => {
							reject(err)
						})
					}
				})
			}
		})
	});
};

export default {
	init,
	login,
	wxLogin
}